package ac.manchester.cs.afzal.autoservd.parser_handler;

import ac.manchester.cs.afzal.autoservd.file_handler.File_Manager;
import ac.manchester.cs.afzal.autoservd.regular_expressions.String_Manipulator;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.Properties;


/**
 * /**
 * Title: Term Classifier
 *
 * Description: A toolkit to find the relevance of candidate terms from a text corpus
 * to any particular topic/sub-domain using a term classification driven approach. The
 * system utilizes the lexical and contextual profiles of the candidate and domain-representing
 * "Resource Terms" (Seed and Ontological). The lexical and contextual profiles of candidate terms
 * are compared with those of Resource Terms to measure their relevance to the topic of interest.
 *
 * TreePruning class converts the
 * <p>Company: The University of Manchester</p>
 *
 * @author Hammad Afzal
 * @version 1.0
 *
 */
public class TreePruning
{

    private Properties props;
    private String MODE = "MODE";
    private String PARSED_SENTENCES_CORPUS = "PARSED_SENTENCES_CORPUS";
    private String PARSED_SENTENCES_TRAINING = "PARSED_SENTENCES_TRAINING";
    private String PARSED_SENTENCES_CORPUS_FILTERED = "PARSED_SENTENCES_CORPUS_FILTERED";
    private String PARSED_SENTENCES_TRAINING_FILTERED = "PARSED_SENTENCES_TRAINING_FILTERED";

    File_Manager fileManager = new File_Manager();
    String_Manipulator strMan = new String_Manipulator();

    public TreePruning()
    {
        try
        {
            props = new Properties();
            props.load(new FileInputStream("Term_Classification.properties"));
            PARSED_SENTENCES_CORPUS = props.getProperty(PARSED_SENTENCES_CORPUS);
            PARSED_SENTENCES_TRAINING = props.getProperty(PARSED_SENTENCES_TRAINING);
            PARSED_SENTENCES_CORPUS_FILTERED = props.getProperty(PARSED_SENTENCES_CORPUS_FILTERED);
            PARSED_SENTENCES_TRAINING_FILTERED = props.getProperty(PARSED_SENTENCES_TRAINING_FILTERED);
            MODE = props.getProperty(MODE);
        }
        catch (IOException ex)
        {
            Logger.getLogger(TreePruning.class.getName()).log(Level.SEVERE, null, ex);
        }


    }

    public void remove_braces_with_contents()
    {
        String[] list_dirs, list_doc;

        File input_file_dir = MODE.equals("Corpus") ? new File(PARSED_SENTENCES_CORPUS) : new File(PARSED_SENTENCES_TRAINING);
        list_dirs = input_file_dir.list();

        for(String dir_name : list_dirs)
        {
            File mkdir = MODE.equals("Corpus") ? new File(PARSED_SENTENCES_CORPUS_FILTERED + "//" + dir_name) : new File(PARSED_SENTENCES_TRAINING_FILTERED + "//" + dir_name);
            mkdir.mkdir();

            File file_doc = MODE.equals("Corpus") ? new File(PARSED_SENTENCES_CORPUS + "//" + dir_name) : new File(PARSED_SENTENCES_TRAINING + "//" + dir_name);
            list_doc = file_doc.list();

            for(String file_name : list_doc)
            {
                String contents;
                StringBuffer file_contents_buffer = new StringBuffer();

                if(MODE.equals("Corpus"))
                {
                    file_contents_buffer = fileManager.fileReader(PARSED_SENTENCES_CORPUS + "//" + dir_name + "//" + file_name);
                }
                else
                {
                    file_contents_buffer = fileManager.fileReader(PARSED_SENTENCES_TRAINING + "//" + dir_name + "//" + file_name);
                }

                contents = file_contents_buffer.toString();

                contents = strMan.string_replacer(contents, "[", "]", "");
                String output_file_name = fileManager.strip_file_name_from_file_name_plus_extension(file_name, ".txt");
                output_file_name = output_file_name + ".xml";
                String output_path = "";

                if(MODE.equals("Corpus"))
                {
                    output_path = PARSED_SENTENCES_CORPUS_FILTERED + "//" + dir_name + "//" + output_file_name;
                }
                else
                {
                    output_path = PARSED_SENTENCES_TRAINING_FILTERED + "//" + dir_name + "//" + output_file_name;
                }

                fileManager.fileWriter(contents, output_path);
                contents = "";
            }
        }
    }
}
